﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coliseu.BussinessLayer;

namespace coliseu.Views.Servicos
{
    public partial class FrmCadFornecedor : Form
    {
        private FornecedorBL fBL;        
        private MunicipioBL mBL = new MunicipioBL();
        private fornecedor f;
        private Fornecedor_ContatoBL fcBL = new Fornecedor_ContatoBL();

        private Tipo_servicoBL tBL = new Tipo_servicoBL();
        private UfBL uBL = new UfBL();

        public FrmCadFornecedor(FornecedorBL fBL)
        {
            InitializeComponent();
            this.fBL = fBL;
            fornecedorBindingSource.AddNew();
            tabControl1.TabPages.RemoveAt(1);
        }

        public FrmCadFornecedor(FornecedorBL fBL, fornecedor f)
        {
            InitializeComponent();
            this.fBL = fBL;
            this.f = f;
            fornecedorBindingSource.DataSource = f;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            List<municipio> m = mBL.selecionar(maskedTextBox1.Text);

            if (m.Count > 0)
            {
                ((fornecedor)fornecedorBindingSource.Current).cidade = m[0].cidade;
                ((fornecedor)fornecedorBindingSource.Current).bairro = m[0].bairro;
                ((fornecedor)fornecedorBindingSource.Current).endereco = m[0].tipo_rua + " " + m[0].rua;
                ((fornecedor)fornecedorBindingSource.Current).uf = m[0].uf;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
          //  ValidateChildren();
            Validate();

            ((fornecedor)fornecedorBindingSource.Current).tipo = comboBox2.Text;
            ((fornecedor)fornecedorBindingSource.Current).avaliacao = comboBox3.Text;
     //       ((fornecedor)fornecedorBindingSource.Current).id_tipo_servico = ((tipo_servico)tiposervicoBindingSource.Current).id_tipo_servico;
            ((fornecedor)fornecedorBindingSource.Current).uf = comboBox5.Text;

            fornecedorBindingSource.EndEdit();

            if (f == null)
            {
                fBL.salvar((fornecedor)fornecedorBindingSource.Current, "insert");
                tabControl1.TabPages.Add(tabPage2);
            }
            else
            {
                fBL.salvar((fornecedor)fornecedorBindingSource.Current, "edit");
                Close();
            }

            MessageBox.Show("Salvo com sucesso", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void FrmCadFornecedor_Load(object sender, EventArgs e)
        {
            tiposervicoBindingSource.DataSource = tBL.selecionar();
            ufBindingSource.DataSource = uBL.selecionar();

            fornecedorcontatoBindingSource.DataSource = fcBL.selecionar(((fornecedor)fornecedorBindingSource.Current).id_fornecedor);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            fornecedorcontatoBindingSource.AddNew();
            groupBox1.Enabled = true;
            comboBox4.Focus();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            ((fornecedor_contato)fornecedorcontatoBindingSource.Current).tipo = comboBox4.Text;
            ((fornecedor_contato)fornecedorcontatoBindingSource.Current).id_fornecedor = ((fornecedor)fornecedorBindingSource.Current).id_fornecedor;
            fornecedorcontatoBindingSource.EndEdit();
            fcBL.salvar((fornecedor_contato)fornecedorcontatoBindingSource.Current);
            fornecedorcontatoBindingSource.DataSource = fcBL.selecionar(((fornecedor)fornecedorBindingSource.Current).id_fornecedor);
            groupBox1.Enabled = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            fcBL.excluir((fornecedor_contato)fornecedorcontatoBindingSource.Current);
            fornecedorcontatoBindingSource.DataSource = fcBL.selecionar(((fornecedor)fornecedorBindingSource.Current).id_fornecedor);
        }
    }
}
